package com.gu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gu.VO.ServerResponse;
import com.gu.VO.UpdatePassword;
import com.gu.VO.UserVO;
import com.gu.constant.Constant;
import com.gu.dao.ApartmentinfoDAO;
import com.gu.dao.UserDAO;
import com.gu.entity.ApartmentinfoDO;
import com.gu.entity.UserDO;
import com.gu.enumeration.UserOccupationTypeEnum;
import com.gu.enumeration.UserStatusEnum;
import com.gu.service.UserService;
import com.gu.utils.UserDO2VO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ProjectName: apartment-management-center
 * @Package: com.gu.service.impl
 * @ClassName: UserServiceImpl
 * @Author: gusongliao
 * @Description: 用户服务实现类
 * @createDate 2021/01/27 16:08
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Override
    public PageInfo userList(Integer pageNum, Integer pageSize,String selectUserParam){
        PageHelper.startPage(pageNum,pageSize);
        List<UserDO> userDOList = userDAO.userList(selectUserParam);
        PageInfo pageInfo = PageInfo.of(userDOList);
        List<UserVO> userVOList = new ArrayList<>();
        for (UserDO userDO : userDOList) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userDO,userVO);
            userVO.setStatus(UserStatusEnum.getUserStatus(userDO.getStatus()));
            userVO.setOccupationType(UserOccupationTypeEnum.getUserOccupation(userDO.getOccupationType()));
            userVOList.add(userVO);
        }
        pageInfo.setList(userVOList);
        return pageInfo;
    }

    @Override
    public ServerResponse updateUserStatus(UserDO userDO) {
        userDO.setStatus(userDO.getStatus() == 1? 0:1);
        int result = userDAO.updateUserStatus(userDO.getStatus(),userDO.getId());
        if (result == 1){
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(4,"更新用户状态失败");
    }

    @Override
    public ServerResponse deleteUserById(Integer id) {
        Integer result = userDAO.deleteUserById(id);
        if (result == 1){
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(5,"删除用户失败");
    }

    @Override
    public ServerResponse insert(UserDO entity) {
        entity.setSystemPassword("666666");
        if (entity.getImgPath() == null){
            entity.setImgPath("http://localhost:8888/3baa4d60-22f8-4089-99a1-3979ff1fe3b9.jpg");
        }
        UserDO userDO = userDAO.selectUserByUserPhone(entity.getUserPhone());
        if (userDO != null){
            return ServerResponse.error(7,"该用户名已经存在，请重新输入");
        }
        int result = userDAO.insert(entity);

        if (result == 1){
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(6,"添加用户失败");
    }

    @Override
    public ServerResponse selectUserByUserPhone(String userPhone, String password, HttpServletRequest request, HttpServletResponse response) {
        UserDO userDO = userDAO.selectUserByUserPhone(userPhone);
        if (userDO == null){
            return ServerResponse.error(6,"用户不存在");
        }
        if (!(userDO.getSystemPassword().equals(password))){
            return ServerResponse.error(7,"密码不正确");
        }
        if (userDO.getStatus() == 0){
            return ServerResponse.error(101,"该用户已封禁",userDO);
        }


        String token = UUID.randomUUID().toString();
        request.getServletContext().setAttribute("token",token);
        Cookie cookie = new Cookie("token", token);
        cookie.setPath("/");
//        if(remember != null){
//            cookie.setMaxAge(Constant.COOKIE_MAX_AGE);
//        }
        response.addCookie(cookie);
        UserVO userVO = UserDO2VO.userDO2UserVO(userDO);
        request.getServletContext().setAttribute("user",userVO);
        //判断当前用户为哪类职业  1 管理员  2  物业维修  3  业主
        return ServerResponse.success(userDO.getOccupationType(),token);
    }

    @Override
    public ServerResponse selectUserByIdCard(String idCard, String userName) {
        UserDO userDO = userDAO.selectUserByIdCard(idCard, userName);
        if (userDO != null){
            userDO.setSystemPassword("666666");
            userDAO.updateAllUserInfo(userDO);
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(25,"输入身份证信息或者姓名不正确,请重新输入");
    }

    @Override
    public ServerResponse updateUserPassword(UpdatePassword updatePassword) {
        if (!(updatePassword.getPassword().equals(updatePassword.getRepassword()))){
            return ServerResponse.error(10,"两次输入密码不相同");
        }
        UserDO userDO = userDAO.selectUserByUserPhone(updatePassword.getUserPhone());
        if (!(userDO.getSystemPassword().equals(updatePassword.getOldPassword()))){
            return ServerResponse.error(11,"原始密码不正确");
        }
        userDAO.updateUserPassword(updatePassword.getPassword(),userDO.getId());
        return ServerResponse.SUCCESS;
    }

    @Override
    public ServerResponse forgetUserPassword(UpdatePassword updatePassword) {

        UserDO userDO = userDAO.selectUserByUserPhone(updatePassword.getUserPhone());
        if (userDO == null){
            return ServerResponse.error(6,"用户不存在");
        }
        if (!(userDO.getIdCard().equals(updatePassword.getIdCard()))){
            return ServerResponse.error(12,"身份证不正确");
        }
        if (!(updatePassword.getPassword().equals(updatePassword.getRepassword()))){
            return ServerResponse.error(10,"两次输入密码不相同");
        }
        userDAO.forgetUserPassword(updatePassword.getPassword(),updatePassword.getUserPhone());
        return ServerResponse.SUCCESS;
    }

    @Override
    public ServerResponse updateUserBanReason(UserDO userDO) {
        int result = userDAO.updateUserBanReason(userDO.getBanReason(),userDO.getId());
        if (result == 1){
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(17,"添加用户封禁原因失败");
    }

    @Override
    public ServerResponse updateAllUserInfo(UserDO userDO) {
        int result = userDAO.updateAllUserInfo(userDO);
        if (result == 1){
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(13,"更新用户失败");
    }

    @Override
    public UserDO getUserById(Integer id) {
        return userDAO.getById(id);
    }


}
